<?PHP
	session_start();
	session_cache_expire(30);
?>
<!--
	calendar.php
	@author Max Palmer and Allen Tucker
	@version 3/26/08, revised 9/15/2008
-->
<html>
	<head>
		<title>
			Calendar display and edit form
		</title>
		<link rel="stylesheet" href="styles.css" type="text/css" />
		<link rel="stylesheet" href="calendar.css" type="text/css" />
	</head>
	<body>
		<div id="container">
			<?PHP include('header.php');?>
			<div id="content">
				<span class="viewweek">
				<?php
					include_once('database/dbWeeks.php');
					include_once('database/dbPersons.php');
					include_once('database/dbLog.php');
					include 'calendar.inc';
               
          echo("<tr><td valign=\"top\"><br>&nbsp;View calendar by month<br></td><td>
                <form method=\"POST\" action=\"calendarMonth.php\" style=\"margin-bottom:0;\">
                <input type=\"hidden\" value=\""View Month"\">");
					// checks to see if in edit mode
					$edit=$_GET['edit'];
					if($edit!="true")
						$edit=false;
					else
						$edit=true;
					// gets the week to show, if no week then defaults to current week
					$weekid=$_GET['id'];
					if(!$weekid)
						$weekid=date("m-d-y",time());
					$week=get_dbWeeks($weekid);
					// if invalid week or unpublished week and not a manager
					if (! $week instanceof Week || $week->get_status()=="unpublished" && $_SESSION['access_level']<2) {
						echo 'This week\'s calendar is not available for viewing. ';
						if ($_SESSION['access_level']>=2)
						   echo ('<a href="addWeek.php"> <br> Manage weeks</a>');
					}
					else {
						// if notes were edited, processes notes
						if(array_key_exists('_submit_check_edit_notes', $_POST) && $_SESSION['access_level']>=2) {
							process_edit_notes($week,$_POST);
							$week=get_dbWeeks($weekid);
						}
						// shows the previous week / next week navigation
						$week_nav=do_week_nav($week,$edit,"");
						echo $week_nav;
						// prevents archived weeks from being edited by anyone
						if($week->get_status()=="archived")
							$edit=false;
						echo '<form method="POST">';
						    $days=$week->get_dates();
							$year=date("Y",time());
							$doy=date("z",time())+1;
						    show_week($days,$week,$edit,$year,$doy);
						    if ($edit==true && !($days[6]->get_year()<$year || ($days[6]->get_year()==$year && $days[6]->get_day_of_year()<$doy) ) && $_SESSION['access_level']>=2)
								echo "<p align=\"center\"><input type=\"submit\" value=\"Save changes to all notes\" name=\"submit\">";
						echo '</form>';
					}
				?>
				<?PHP include('footer.inc');?>
			</div>
		</div>
	</body>
</html>

<?php
	/**
	* shows the "previous week"/"next week" menu, if the weeks exist
	*/
	/**
	* gets a week object, and displays it as a calendar
	*/
	function show_week($days,$week,$edit,$year,$doy) {
		// gets all of the dates for this week
		// sets up the table, with title, and then day of month
		$daynames = array ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
		echo ('<table id="calendar" align="center" ><tr class="weekname"><td colspan="16" ' .
				'align="center" >'.'House Calendar: '.$week->get_name().'</td></tr>
				<tr><td bgcolor="#7991B1"></td>');
		for($i=0; $i<7; $i++)
			echo ('<td class="dom">'.get_dom($days[$i]).'</td><td class="dow">&nbsp; '. $daynames[$i] .'</td>');
		echo('<td bgcolor="#7991B1"></td></tr>');
		$free_hour = array();
		for ($i=0; $i<84; $i++)
		    $free_hour[] = true;
		for ($hour=9; $hour < 21; $hour++) {
			$clock = $hour < 12 ? $hour . "am" : $hour -12 . "pm";
			if ($clock == "0pm") $clock = "12pm";
			echo ("<tr><td class=\"hour\">".$clock."</td>");
			for ($i=0; $i<7; $i++){
				$shiftid = $days[$i]->get_shift_id($hour);
				if ($shiftid) {
					$shift_length = get_shift_end($shiftid)-get_shift_start($shiftid);
					echo do_shift($days[$i],substr($shiftid,9),
					    $shift_length,$edit);
					for ($j=$hour; $j<$hour+$shift_length; $j++)
					    $free_hour[7*($j-9)+$i] = false;
				}
				else if ($free_hour[7*($hour-9)+$i])
				    echo "<td colspan='2' bgcolor=\"lightgray\"></td>";

			}
			echo ("<td class=\"hour\">".$clock."</td></tr>");
		}
// night
		echo ("<tr><td class=\"hour\">night</td>");
		for ($i=0; $i<4; $i++)
		  echo ("<td colspan=\"2\" rowspan=\"1\" bgcolor=\"lightgray\">&nbsp;</td>");
		echo (do_shift($days[4],"night",1,$edit));
		echo (do_shift($days[5],"night",1,$edit));
		echo ("<td colspan=\"2\" rowspan=\"1\" bgcolor=\"lightgray\">&nbsp;</td><td class=\"hour\">night</td></tr>");

// notes and guest chef
		echo do_day_notes($days,$edit, $year, $doy);
		echo "<input type=\"hidden\" name=\"_submit_check_edit_notes\" value=\"1\">";
		echo "<input type=\"hidden\" name=\"weekid\" value=\"".$days[0]->get_id()."\">";

		echo "</table>";
	}


	?>